<?php
	set_time_limit(60*60);
	ini_set('memory_limit', '128M');

	require_once "config.php";
	require_once dirname(__FILE__). '/util/connection.class.php';
	require_once dirname(__FILE__). '/util/TimeUtil.php';
	
	
	$dbconn = Connection::getInstance();
	
	
	$sent_date = '2012-01-11';
	$emailId_Uid = getEmailId_Offer_Uid($sent_date, $dbconn);		var_dump($emailId_Uid);
	
	//updateEmail_links($emailId_Uid, $dbconn);
	
	
	
	
	
	
	
	
	
	
	
	
	function getEmailId_Offer_Uid($sent_date, Connection $db)
	{
		$sql = " SELECT e.id AS emailId, u.id AS uid  FROM emails e LEFT JOIN users u ON e.sendto = u.email WHERE sent_date>=? ";
		try{
			$emailId_Uid = $db->query($sql, array($sent_date));
			return $emailId_Uid;
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	function updateEmail_links($emailId_Uid, Connection $db)
	{
		try{
			if(!empty($emailId_Uid)){
				foreach($emailId_Uid as $k=>$e_u){
					if($e_u['uid'] != ''){
						$sql = " UPDATE email_links SET offer1_link=replace(offer1_link, 'uid=0-', 'uid=" . $e_u['uid'] . "-'), offer2_link=replace(offer2_link, 'uid=0-', 'uid=" . $e_u['uid'] . "-'), offer3_link=replace(offer3_link, 'uid=0-', 'uid=" . $e_u['uid'] . "-') WHERE email_id={$e_u['emailId']} ";
						$db->exec($sql);
					}
				}
			}			
		}catch (Exception $e){
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}

		
	}